package com.yidianyanglao.onlinemall.controller;

import com.yidianyanglao.core.bean.RetBean;
import com.yidianyanglao.core.enums.Code;
import com.yidianyanglao.core.exception.CustomException;
import com.yidianyanglao.onlinemall.bean.OnlineBannerBean;
import com.yidianyanglao.onlinemall.bean.OnlineMallBean;
import com.yidianyanglao.onlinemall.mapper.OnlineMallMapper;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * Created by Seed on 2016/12/21.
 */
@Controller
public class OnlineMallController {

    @Resource
    private OnlineMallMapper omm;


    /**
     * 获取首页banner列表
     * @return
     */
    @RequestMapping(value = "/online/getOnlineBanner.json")
    @ResponseBody
    public RetBean getBanner(){


        List<OnlineBannerBean> bannerList=omm.getBanner();

        return new RetBean(Code.SUCCESS,"请求成功",bannerList);
    }

    /**
     * 获取在线商城列表
     * @return
     */
    @RequestMapping(value = "/online/getShopList.json")
    @ResponseBody
    public RetBean getMallList(String pageNum,String pageSize){


        int offset=pageSize==null?10:Integer.parseInt(pageSize);
        int start=(pageNum==null?1:Integer.parseInt(pageNum)-1)*offset;

        List<OnlineMallBean> mallList=omm.getShopList(start,offset);

        return new RetBean(Code.SUCCESS,"请求成功",mallList);
    }

    /**
     * 新增banner
     * @param banner
     * @param url
     * @param sort
     */
    @RequestMapping(value = "/online/addBanner.json")
    @ResponseBody
    public RetBean addBanner(String banner, String url, String sort){

        OnlineBannerBean obb=new OnlineBannerBean();

        obb.setBanner(banner);
        obb.setUrl(url);
        obb.setSort(Integer.parseInt(sort));
        obb.setCreatedate(new Date());
        int isOk=omm.addNewBanner(obb);
        if(isOk<=0){
            throw  new CustomException(Code.ERROR,"新增失败");
        }else{
            return  new RetBean(Code.SUCCESS,"新增成功",null);
        }

    }

    /**
     * 修改banner
     * @param banner
     * @param url
     * @param sort
     * @param id
     * @return
     */
    @RequestMapping(value = "/online/updateBanner.json")
    @ResponseBody
    public RetBean uddateBanner(String banner,String url,String sort,String id){

        OnlineBannerBean obb=new OnlineBannerBean();

        obb.setBanner(banner);
        obb.setUrl(url);
        obb.setSort(Integer.parseInt(sort));
        obb.setCreatedate(new Date());
        obb.setUid(Integer.parseInt(id));

        int isOk=omm.updateBanner(obb);
        if(isOk<=0){
            throw  new CustomException(Code.ERROR,"修改失败");
        }else{
            return  new RetBean(Code.SUCCESS,"修改成功",null);
        }
    }


}
